home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1998 June / Cd Pc Users 9.iso / prog / inst / baslibs / clslinke.cls < prev    next >
Encoding:
Text File  |  1996-12-04  |  6.3 KB  |  282 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "clsLinkedList"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. '
  11. '  A simple doublely linked list class
  12. '
  13.  
  14. Private m_head As clsListItem
  15. Private m_tail As clsListItem
  16. Private m_cur  As clsListItem
  17.  
  18. Private m_count As Long
  19. '
  20. '  Adds ItemData to the head of the linked list
  21. '
  22. Public Sub AddFirst(ItemData As Variant)
  23.     Dim Item As New clsListItem
  24.  
  25.     If VarType(ItemData) = vbObject Then
  26.        Set Item.ItemData = ItemData
  27.     Else
  28.        Item.ItemData = ItemData
  29.     End If
  30.  
  31.     If (m_head Is Nothing) Then
  32.         Set m_head = Item
  33.         Set m_tail = Item
  34.         Set m_cur = Item
  35.     Else
  36.         Set Item.NextItem = m_head
  37.         Set Item.PrevItem = Nothing
  38.         Set Item.NextItem.PrevItem = Item
  39.         Set m_head = Item
  40.     End If
  41.     
  42.     m_count = m_count + 1
  43. End Sub
  44.  
  45. '
  46. '  Adds ItemData to the end of the linked list.
  47. '
  48. Public Sub AddLast(ItemData As Variant)
  49.     Dim Item As New clsListItem
  50.  
  51.     If VarType(ItemData) = vbObject Then
  52.        Set Item.ItemData = ItemData
  53.     Else
  54.        Item.ItemData = ItemData
  55.     End If
  56.  
  57.     If (m_tail Is Nothing) Then
  58.         Set m_head = Item
  59.         Set m_tail = Item
  60.         Set m_cur = Item
  61.     Else
  62.         Set Item.PrevItem = m_tail
  63.         Set Item.NextItem = Nothing
  64.         Set Item.PrevItem.NextItem = Item
  65.         Set m_tail = Item
  66.     End If
  67.     
  68.     m_count = m_count + 1
  69. End Sub
  70.  
  71. '
  72. '  Returns the number of items in the linked list.
  73. '
  74. Property Get Count() As Long
  75.    Count = m_count
  76. End Property
  77.  
  78. '
  79. '  Returns the current item.
  80. '
  81. Property Get CurrentItem() As Variant
  82.    If m_cur Is Nothing Then
  83.       CurrentItem = Null
  84.    Else
  85.       If VarType(m_cur.ItemData) = vbObject Then
  86.          Set CurrentItem = m_cur.ItemData
  87.       Else
  88.          CurrentItem = m_cur.ItemData
  89.       End If
  90.    End If
  91. End Property
  92.  
  93. '
  94. '  Sets the current item.
  95. '
  96. Property Let CurrentItem(ItemData As Variant)
  97.    If Not m_cur Is Nothing Then
  98.       m_cur.ItemData = ItemData
  99.    End If
  100. End Property
  101. Property Set CurrentItem(ItemData As Variant)
  102.    If Not m_cur Is Nothing Then
  103.       Set m_cur.ItemData = ItemData
  104.    End If
  105. End Property
  106. '
  107. '  Inserts ItemData after the current item in the list.
  108. '
  109. Public Sub InsertAfter(ItemData As Variant)
  110.     Dim Item As New clsListItem
  111.  
  112.     If VarType(ItemData) = vbObject Then
  113.        Set Item.ItemData = ItemData
  114.     Else
  115.        Item.ItemData = ItemData
  116.     End If
  117.  
  118.     If (m_cur Is Nothing) Then
  119.         Set m_head = Item
  120.         Set m_tail = Item
  121.         Set m_cur = Item
  122.     Else
  123.         Set Item.NextItem = m_cur.NextItem
  124.         Set Item.PrevItem = m_cur
  125.         Set m_cur.NextItem = Item
  126.         'Add the following line.
  127.         Set m_cur.NextItem.PrevItem = Item
  128.         
  129.         If (m_cur.NextItem Is Nothing) Then
  130.             Set m_tail = m_cur
  131.         End If
  132.     End If
  133.     
  134.     m_count = m_count + 1
  135. End Sub
  136.  
  137. '
  138. '  Delete's the
  139. '
  140. Public Sub DeleteAll()
  141.    Dim m As clsListItem
  142.    Dim m2 As clsListItem
  143.    
  144.    m = m_head
  145.    
  146.    Do While Not (m Is Nothing)
  147.       Set m2 = m.NextItem
  148.       Set m.NextItem = Nothing
  149.       Set m.PrevItem = Nothing
  150.       Set m = m2
  151.    Loop
  152.    
  153.    m_head = Nothing
  154.    m_tail = Nothing
  155.    m_cur = Nothing
  156.    m_count = 0
  157. End Sub
  158.    
  159. Public Sub DeleteCurrent()
  160.     Dim tmp As clsListItem
  161.  
  162.     If (m_cur Is Nothing) Then
  163.         Exit Sub
  164.     End If
  165.  
  166.     If (m_cur.PrevItem Is Nothing) Then
  167.         '
  168.         ' Delete head of list
  169.         '
  170.         Set m_head = m_cur.NextItem
  171.         If (m_head Is Nothing) Then
  172.             '
  173.             ' Also deleting tail, list becomes empty
  174.             '
  175.             Set m_tail = Nothing
  176.             Set m_cur = Nothing
  177.         Else
  178.             Set m_head.PrevItem = Nothing
  179.             Set m_cur = m_head
  180.         End If
  181.     ElseIf (m_cur.NextItem Is Nothing) Then
  182.         '
  183.         ' Deleting end of list
  184.         '
  185.         Set m_tail = m_cur.PrevItem
  186.         If (m_tail Is Nothing) Then
  187.             '
  188.             ' Also deleting head, list becomes empty
  189.             '
  190.             Set m_head = Nothing
  191.             Set m_cur = Nothing
  192.         Else
  193.             Set m_cur = m_tail
  194.             Set m_cur.NextItem = Nothing
  195.         End If
  196.     Else
  197.         '
  198.         ' Delete somewhere inside of list
  199.         '
  200.         Set tmp = m_cur.NextItem
  201.         Set m_cur.PrevItem.NextItem = m_cur.NextItem
  202.         Set m_cur.NextItem.PrevItem = m_cur.PrevItem
  203.         Set m_cur = tmp
  204.     End If
  205.     
  206.     m_count = m_count - 1
  207. End Sub
  208. '
  209. '  Return's the first item in the list.
  210. '
  211. Public Function FirstItem() As Variant
  212.     If (m_head Is Nothing) Then
  213.         FirstItem = Null
  214.     Else
  215.         If (VarType(m_head.ItemData) = vbObject) Then
  216.             Set FirstItem = m_head.ItemData
  217.         Else
  218.             FirstItem = m_head.ItemData
  219.         End If
  220.         Set m_cur = m_head
  221.     End If
  222. End Function
  223.  
  224.  
  225. '
  226. '  Returns the next item in the list.
  227. '
  228. Public Function NextItem() As Variant
  229.     If (m_cur Is Nothing) Then
  230.         NextItem = Null
  231.         Debug.Print "First Null"
  232.     Else
  233.         If (m_cur Is Nothing) Then
  234.             NextItem = Null
  235.         Else
  236.             Set m_cur = m_cur.NextItem
  237.             If (VarType(m_cur.ItemData) = vbObject) Then
  238.                 Set NextItem = m_cur.ItemData
  239.             Else
  240.                 NextItem = m_cur.ItemData
  241.             End If
  242.         End If
  243.     End If
  244. End Function
  245.  
  246. '
  247. ' Returns the last item in the list.
  248. '
  249. Public Function LastItem() As Variant
  250.     If (m_tail Is Nothing) Then
  251.         LastItem = Null
  252.     Else
  253.         Set m_cur = m_tail
  254.         If (VarType(m_cur.ItemData) = vbObject) Then
  255.             Set LastItem = m_cur.ItemData
  256.         Else
  257.             LastItem = m_cur.ItemData
  258.         End If
  259.     End If
  260. End Function
  261.  
  262. '
  263. '  Returns the previous item in the list.
  264. '
  265. Public Function PrevItem() As Variant
  266.     If (m_cur Is Nothing) Then
  267.         PrevItem = Null
  268.     Else
  269.         If (m_cur.PrevItem Is Nothing) Then
  270.             PrevItem = Null
  271.         Else
  272.             Set m_cur = m_cur.PrevItem
  273.             If (VarType(m_cur.ItemData) = vbObject) Then
  274.                 Set PrevItem = m_cur.ItemData
  275.             Else
  276.                 PrevItem = m_cur.ItemData
  277.             End If
  278.         End If
  279.     End If
  280. End Function
  281.  
  282.